package com.otod.pm.doc.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
 













import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.otod.base.exception.AppException;
import com.otod.base.json.JsonSuccess;
import com.otod.commons.utils.StringUtils;
import com.otod.commons.web.BaseController;
import com.otod.pm.doc.service.DocTpMasterService;
import com.otod.pm.doc.service.DocTpService;
import com.otod.pm.doc.vo.PmDocTpItemVo;
import com.otod.pm.doc.vo.PmDocTpMasterVo;
import com.otod.pm.prj.service.ProjectService;
import com.otod.pm.prj.vo.ProjectVo;


@Controller
@RequestMapping("pm/doc/item")
public class DocTpController extends BaseController {
	
	@Autowired
	private DocTpService docTpService;
	
	@Autowired
	private DocTpMasterService docTpMasterService;
	
	@Autowired
	
	private ProjectService projectService;
	
	
	/**
	 * 保存文档模板
	 * @throws AppException 
	 */
	@RequestMapping(value = "savePmDocTp")
	@ResponseBody
	public String savePmDocTp(PmDocTpMasterVo pmDocTpMasterVo,HttpServletRequest request) throws AppException{
		if(StringUtils.isEmpty(pmDocTpMasterVo.getUuid())){
			//查询模板是否重复
			docTpMasterService.selectRepetitionTpName(pmDocTpMasterVo);
			docTpMasterService.insert(pmDocTpMasterVo);
		}else{
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("uuid", pmDocTpMasterVo.getUuid());
			map.put("tpName", pmDocTpMasterVo.getTpName());
			
			docTpMasterService.update(map);
		}
		
		return JsonSuccess.factory("文档模板保存成功！");
	}
	
	/**
	 * 根据项目类型获取模板信息
	 * @param prjType
	 * @return
	 */
	@RequestMapping(value="getDocTpByPrjType",method=RequestMethod.POST)
	@ResponseBody
	public List<PmDocTpMasterVo> getDocTpByPrjType(String prjType,String initPrjType) {
		Map<String,Object> params = new HashMap<String,Object>(); 
		
		if(StringUtils.isEmpty(prjType)){
			params.put("prjType", initPrjType);
		}else{
			params.put("prjType", prjType);
		}
		return docTpMasterService.queryForList(params);
   }
	/**
	 * 根据项目类型获取模板信息 通过项目的id查询项目的类型
	 * @param id
	 * @return
	 */
	@RequestMapping(value="getDocTpPrjType",method=RequestMethod.POST)
	@ResponseBody
	public List<PmDocTpMasterVo> getDocTpPrjType(@RequestBody String id) {
		Map<String,Object> params = new HashMap<String,Object>(); 
		
		ProjectVo projectVo = projectService.getById(id);
		params.put("prjType", projectVo.getPrjType());
		
		return docTpMasterService.queryForList(params);
   }
	
	/**
	 * 根据项目类型获取模板信息
	 * @param id
	 * @return
	 */
	@RequestMapping(value="getDocTpMasterPrjType",method=RequestMethod.POST)
	@ResponseBody
	public List<PmDocTpMasterVo> getDocTpMasterPrjType(@RequestBody String prjType) {
		Map<String,Object> params = new HashMap<String,Object>(); 
		
		
		params.put("prjType", prjType);
		
		return docTpMasterService.queryForList(params);
   }
	
	
	/**
	 * 通过模板id查询数据
	 * @param tpId
	 * @return
	 */
	@RequestMapping(value="getItemBytpId",method=RequestMethod.POST)
	@ResponseBody
	public List<PmDocTpItemVo> getItemBytpId(String tpId) {
		
		Map<String,Object> params = new HashMap<String,Object>(); 
		if(StringUtils.isNotEmpty(tpId)){
			params.put("tpId", tpId);
		}
		
		return docTpService.queryForList(params);
   }
	
	/**
	 * 根据项目类型获取模板信息
	 * @param prjType
	 * @return
	 */
	@RequestMapping(value="getItemById",method=RequestMethod.POST)
	@ResponseBody
	public PmDocTpItemVo getItemById(@RequestBody String id,HttpServletRequest request) {
		
		return docTpService.getById(id);
   }
	
	/**
	 * 查询文档树当前item的所有子节点
	 * @return 
	 */
	@RequestMapping(value = "getAllChildItemList", method = RequestMethod.POST)
	@ResponseBody
	public List<PmDocTpItemVo> getAllChildItemList(String id,String tpId) {
	
		return docTpService.queryChildrenForList(id,tpId);
	}

	/**
	 * 保存
	 * @throws AppException 
	 */
	@RequestMapping(value = "save")
	@ResponseBody
	public String save(PmDocTpItemVo docTpItemVo,HttpServletRequest request) throws AppException{
		if(StringUtils.isEmpty(docTpItemVo.getUuid())){
			//查询名字是否重复
			docTpService.selectItemTitle(docTpItemVo);
			docTpService.insert(docTpItemVo);
		}else{
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("uuid", docTpItemVo.getUuid());
			map.put("title", docTpItemVo.getTitle());
			if(StringUtils.isNotEmpty(docTpItemVo.getDocStyle())){
				map.put("docStyle", docTpItemVo.getDocStyle());
			}
			if(docTpItemVo.getIndexNo() != null){
				map.put("indexNo", docTpItemVo.getIndexNo());
			}
			if(docTpItemVo.getLvl()!= null){
				map.put("lvl", docTpItemVo.getLvl());
			}
			
			
			if(StringUtils.isNotEmpty(docTpItemVo.getParentId())){
				map.put("parentId", docTpItemVo.getParentId());
			}

			
			
			map.put("tpId", docTpItemVo.getTpId());
			docTpService.update(map);
		}
		
		return JsonSuccess.factory("条目保存成功！");
	}
	
	
	/**
	 * 删除
	 * @throws AppException 
	 */
	@RequestMapping(value = "deleteById", method = RequestMethod.POST)
	@ResponseBody
	public String deleteById(@RequestBody String itemId) throws AppException{
		
		docTpService.deleteById(itemId);
		
		return JsonSuccess.factory("条目删除成功！");
	}
	/**
	 * 删除
	 * @throws AppException 
	 */
	@RequestMapping(value = "deleteByTpId", method = RequestMethod.POST)
	@ResponseBody
	public String deleteByTpId(@RequestBody String tpId) throws AppException{
		
		docTpMasterService.deleteById(tpId);
		
		return JsonSuccess.factory("条目删除成功！");
	}
	
	/**
	 * 删除
	 * @throws AppException 
	 */
	@RequestMapping(value = "deleteId", method = RequestMethod.POST)
	@ResponseBody
	public String deleteId(String uuid) throws AppException{
		
		docTpService.deleteById(uuid);
		
		return JsonSuccess.factory("条目删除成功！");
	}
	
}
